package com.example.bailin.giftsay.main;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseActivity;
import com.example.bailin.giftsay.entity.SelectGiftEntity;
import com.example.bailin.giftsay.entity.SelectPopupEntity;
import com.example.bailin.giftsay.guide.PopupClick;
import com.example.bailin.giftsay.main.adapter.PxAdapter;
import com.example.bailin.giftsay.main.adapter.SelectGiftAdapter;
import com.example.bailin.giftsay.main.adapter.ShowAllAdapter;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;
import com.example.bailin.giftsay.tool.UrlProvide;

/**
 * Created by bailin on 16/8/24.
 * 选礼神器
 */
public class SelectActivity extends BaseActivity implements View.OnClickListener, SXpupListener {
    private RecyclerView rv;
    private RelativeLayout rlDX;
    private RelativeLayout rlCH;
    private RelativeLayout rlJG;
    private RelativeLayout rlGX;
    private TextView tvDX;
    private TextView tvCH;
    private TextView tvGX;
    private TextView tvJG;
    private ImageView ivDX;
    private ImageView ivCH;
    private ImageView ivGX;
    private ImageView ivJG;
    private ImageView ivReturn;
    private PopupWindow popupWindow;
    private RecyclerView rvPopup;
    private int a, b, c, d;
    private ImageView ivSelectPaixu;
    private SelectGiftAdapter selectGiftAdapter;
    private ShowAllAdapter adapter;
    private GridLayoutManager manager;
    private int order;

    @Override
    protected int setLayout() {
        return R.layout.activity_select;
    }

    @Override
    protected void initView() {
        rv = bindView(R.id.rv_xlsq_id);
        rlDX = bindView(R.id.rl_click_duixiang);
        rlCH = bindView(R.id.rl_click_changhe);
        rlGX = bindView(R.id.rl_click_gexing);
        rlJG = bindView(R.id.rl_click_jiage);
        rlDX.setOnClickListener(this);
        rlCH.setOnClickListener(this);
        rlGX.setOnClickListener(this);
        rlJG.setOnClickListener(this);
        tvDX = bindView(R.id.tv_select_duixiang);
        tvCH = bindView(R.id.tv_select_changhe);
        tvGX = bindView(R.id.tv_select_gexing);
        tvJG = bindView(R.id.tv_select_jiage);
        ivDX = bindView(R.id.iv_select_duixiang);
        ivCH = bindView(R.id.iv_select_changhe);
        ivGX = bindView(R.id.iv_select_gexing);
        ivJG = bindView(R.id.iv_select_jiage);
        ivReturn = bindView(R.id.iv_select_return);
        ivSelectPaixu = bindView(R.id.iv_select_paixu);
        ivSelectPaixu.setOnClickListener(this);
        ivReturn.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        adapter = new ShowAllAdapter();
        manager = new GridLayoutManager(MyApp.getContext(), 2);
        tool().getData(UrlProvide.SELECT_GIFT, SelectGiftEntity.class, new NetTool.NetInterface<SelectGiftEntity>() {
            @Override
            public void onSuccess(SelectGiftEntity selectGiftEntity) {
                adapter.setEntity(selectGiftEntity);
                rv.setLayoutManager(manager);
                rv.setAdapter(adapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_click_duixiang:
                ivDX.setImageResource(R.mipmap.arrow_grey_up);
                initmPopupWindowView(0, v);
                break;
            case R.id.rl_click_changhe:
                ivCH.setImageResource(R.mipmap.arrow_grey_up);
                initmPopupWindowView(1, v);
                break;
            case R.id.rl_click_gexing:
                ivGX.setImageResource(R.mipmap.arrow_grey_up);
                initmPopupWindowView(2, v);
                break;
            case R.id.rl_click_jiage:
                ivJG.setImageResource(R.mipmap.arrow_grey_up);
                initmPopupWindowView(3, v);
                break;
            case R.id.iv_select_return:
                finish();
                break;
            case R.id.iv_select_paixu:
                initPopupWindowPaixu(v);

                break;
        }
    }

    private void initPopupWindowPaixu(View v) {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            return;
        } else {
            View customView1 = getLayoutInflater().inflate(R.layout.item_paixu_listview, null);
            ListView lv = (ListView) customView1.findViewById(R.id.lv_paixu);
            final PxAdapter pxAdapter = new PxAdapter();
            pxAdapter.setPopupClick(new PopupClick() {
                @Override
                public void itemOnClick(int position) {
                    popupWindow.dismiss();
                    adapter.setIndex(position);
                    order = position;
                }
            });
            pxAdapter.setIndex(order);
            lv.setAdapter(pxAdapter);
            popupWindow = new PopupWindow(customView1, 450, ViewGroup.LayoutParams.WRAP_CONTENT);
            customView1.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (popupWindow != null && popupWindow.isShowing()) {
                        popupWindow.dismiss();
                        popupWindow = null;
                    }
                    return false;
                }
            });
            popupWindow.setFocusable(true);
        }
        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, 970, 230);
        popupWindow.showAsDropDown(v, 0, 5);
    }

    private void initmPopupWindowView(final int index, final View v) {
        tool().getData(UrlProvide.SELECT_POPUP, SelectPopupEntity.class, new NetTool.NetInterface<SelectPopupEntity>() {
            @Override
            public void onSuccess(SelectPopupEntity selectPopupEntity) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    return;
                } else {
                    View customView = getLayoutInflater().inflate(R.layout.item_rv_popup, null);
                    rvPopup = (RecyclerView) customView.findViewById(R.id.rv_popup_id);
                    GridLayoutManager manager = new GridLayoutManager(MyApp.getContext(), 3);
                    rvPopup.setLayoutManager(manager);
                    selectGiftAdapter = new SelectGiftAdapter();
                    selectGiftAdapter.setIndex(index);
                    selectGiftAdapter.setsXpupListener(SelectActivity.this);
                    if (index == 0) {
                        selectGiftAdapter.setSelect(a);
                    }
                    if (index == 1) {
                        selectGiftAdapter.setSelect(b);
                    }
                    if (index == 2) {
                        selectGiftAdapter.setSelect(c);
                    }
                    if (index == 3) {
                        selectGiftAdapter.setSelect(d);
                    }
                    selectGiftAdapter.setEntity(selectPopupEntity);
                    rvPopup.setAdapter(selectGiftAdapter);
                    popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.MATCH_PARENT, 630);
                    // 监听PopupWindow消失
                    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            ivDX.setImageResource(R.mipmap.arrow_grey_down);
                            ivCH.setImageResource(R.mipmap.arrow_grey_down);
                            ivGX.setImageResource(R.mipmap.arrow_grey_down);
                            ivJG.setImageResource(R.mipmap.arrow_grey_down);
                        }
                    });
                    customView.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (popupWindow != null && popupWindow.isShowing()) {
                                popupWindow.dismiss();
                                popupWindow = null;
                            }
                            return false;
                        }
                    });
//             点击popupwindow其他地方会消失
                    popupWindow.setFocusable(true);
                }
                popupWindow.showAsDropDown(v, 0, 5);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

    private String DX = "";
    private String CH = "";
    private String GX = "";
    private String JG = "";

    @Override
    public void onClick(int position, int index, String type, String key, String name) {
        switch (index) {
            case 0:
                a = position;
                if (position == 0) {
                    DX = "";
                    tvDX.setText("对象");
                } else {
                    DX = "&" + type + "=" + key;
                    tvDX.setText(name);
                }
                break;
            case 1:
                b = position;
                if (position == 0) {
                    CH = "";
                    tvCH.setText("场合");
                } else {
                    CH = "&" + type + "=" + key;
                    tvCH.setText(name);
                }
                break;
            case 2:
                c = position;
                if (position == 0) {
                    GX = "";
                    tvGX.setText("个性");
                } else {
                    GX = "&" + type + "=" + key;
                    tvGX.setText(name);
                }
                break;
            case 3:
                d = position;
                if (position == 0) {
                    JG = "";
                    tvJG.setText("价格");
                } else {
                    JG = "&" + type + "=" + key;
                    tvJG.setText(name);
                }
                break;
        }

        tool().getData(UrlProvide.SELECT_GIFT + DX + CH + GX + JG, SelectGiftEntity.class, new NetTool.NetInterface<SelectGiftEntity>() {
            @Override
            public void onSuccess(SelectGiftEntity selectGiftEntity) {
                adapter.setEntity(selectGiftEntity);
                rv.setLayoutManager(manager);
                rv.setAdapter(adapter);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
        popupWindow.dismiss();
    }
}